package gsp420.game;

import java.awt.Graphics;

public class Rect
{
	/** position */
	public Point p = new Point();
	/** dimension */
	public Point d = new Point();
	
	void set(Entity e)
	{
		// pointing to the exact same memory
		//p = e.p;
		// copying the value at that location
		p.set(e.p);
		d.set(e.d);
	}
	void set(Rect r)
	{
		// pointing to the exact same memory
		//p = r.p;
		// copying the value at that location
		p.set(r.p);
		d.set(r.d);
	}
	void invalidate()
	{
		d.set(0,0);
	}
	boolean isValid()
	{
		return d.x > 0 && d.y > 0;
	}
	
	void draw(Graphics g)
	{
		g.drawRect((int)p.x, (int)p.y, (int)d.x-1, (int)d.y-1);
	}
	
	float getLeft()		{	return p.x;		}
	float getRight()	{	return p.x+d.x;	}
	float getUp()		{	return p.y;		}
	float getDown()		{	return p.y+d.y;	}
	void setLeft(float val){p.x = val;		}
	void setRight(float val){d.x = val-p.x;	}
	void setUp(float val){p.y = val;		}
	void setDown(float val){d.y = val-p.y;	}
	
	void add(Rect r)
	{
		if(r.getLeft() < getLeft())		setLeft(r.getLeft());
		if(r.getRight() > getRight())	setRight(r.getRight());
		if(r.getUp() < getUp())			setUp(r.getUp());
		if(r.getDown() > getDown())		setDown(r.getDown());
	}
	
	public String toString()
	{
		return "["+p.x+","+p.y+";"+d.x+","+d.y+"]";
	}
}
